//
//  ViewController.m
//  RunTimeDemo
//
//  Created by 曾墨 on 16/9/9.
//  Copyright © 2016年 YuanJiang. All rights reserved.
//

#import "ViewController.h"

#import <objc/runtime.h>

static void *YJKey=@"ShenYuanJiang";

@interface ViewController ()<UIAlertViewDelegate>

@end
/**
 *  runTime  runRoop 的区别
 */
@implementation ViewController


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self showAlert];
}

-(void) showAlert
{
    //控制器
//    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"YuanJiangQuits" message:@"someThingsInsteresting" preferredStyle:UIAlertControllerStyleAlert];
//    
//   UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//       NSLog(@"222");
//   }];
//    UIAlertAction *addAction = [UIAlertAction actionWithTitle:@"点我继续" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        NSLog(@"1111");
//    }];
//    [alertVC addAction:cancel];
//    [alertVC addAction:addAction];
//   [self presentViewController:alertVC animated:YES completion:^{
//       ;
//   }];
    
    //alertView
    UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"提示" message:@"啥都没有" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    
    void (^block)(NSInteger) = ^(NSInteger buttonIndex){
        if (buttonIndex==0) {
            NSLog(@"cancel");
        }else if (buttonIndex==1){
            NSLog(@"addAction");
        }
        
    };
    
    objc_setAssociatedObject(alert, YJKey, block , OBJC_ASSOCIATION_COPY);
    [alert show];
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    void (^block)(NSInteger) = objc_getAssociatedObject(alertView, YJKey);
    block(buttonIndex);
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
}

@end



















